package me.siphone.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.hjq.http.EasyHttp;
import com.hjq.http.config.IRequestApi;
import com.hjq.http.listener.HttpCallbackProxy;
import java.util.ArrayList;
import java.util.List;
import me.siphone.http.api.VoiceCallApi;
import me.siphone.http.model.HttpData;
import me.siphone.sip.SipUtil;
import me.siphone.view.grouptree.Device;
import me.siphone.view.grouptree.GroupTreeFragment;
import org.linphone.R;

public class AudioCallFragment extends BaseFragment {

    private Button btnCall;

    @Nullable
    @Override
    public View onCreateView(
            @NonNull LayoutInflater inflater,
            @Nullable ViewGroup container,
            @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_audio_call, container, false);

        btnCall = view.findViewById(R.id.btn_call);

        btnCall.setOnClickListener(
                v -> {
                    btnCall.setClickable(false);
                    if (getFragmentManager() != null) {
                        GroupTreeFragment fragment =
                                (GroupTreeFragment)
                                        getChildFragmentManager()
                                                .findFragmentById(R.id.group_tree_fragment);
                        List<Device> selectedDevices = fragment.getSelectedDeviceList();
                        Log.d("sisyphus", "选中设备：" + selectedDevices);
                        startVoiceCall(selectedDevices);
                    } else {
                        btnCall.setClickable(true);
                    }
                });
        return view;
    }

    public void startVoiceCall(List<Device> deviceList) {
        if (deviceList.isEmpty()) {
            Toast.makeText(getContext(), "请至少选择一个设备", Toast.LENGTH_SHORT).show();
            btnCall.setClickable(true);
            return;
        }

        if (!SipUtil.isRegister()) {
            Toast.makeText(getContext(), "未注册，请稍后", Toast.LENGTH_SHORT).show();
            btnCall.setClickable(true);
            return;
        }

        List<String> numbers = new ArrayList<>();
        for (Device device : deviceList) {
            numbers.add(device.number);
        }

        String number = SipUtil.getActiveAuthNumber();
        if (!numbers.contains(number)) {
            numbers.add(number);
        }

        EasyHttp.post(this)
                .api(new VoiceCallApi(numbers, "", true))
                .request(
                        new HttpCallbackProxy<HttpData<VoiceCallApi.Data>>(this) {
                            @Override
                            public void onHttpSuccess(@NonNull HttpData<VoiceCallApi.Data> result) {
                                super.onHttpSuccess(result);
                                if (result.getCode() == 200 && result.getData() != null) {
                                    SipUtil.saveActiveCallNumber(
                                            result.getData().getConferenceNumber());
                                    Log.d(
                                            "sisyphus",
                                            "记录会议号码：" + result.getData().getConferenceNumber());

                                    if (getFragmentManager() != null) {
                                        GroupTreeFragment fragment =
                                                (GroupTreeFragment)
                                                        getChildFragmentManager()
                                                                .findFragmentById(
                                                                        R.id.group_tree_fragment);
                                        fragment.clearSelected();
                                    }
                                }
                            }

                            @Override
                            public void onHttpEnd(@NonNull IRequestApi api) {
                                super.onHttpEnd(api);
                                btnCall.setClickable(true);
                            }
                        });
    }
}
